package com.example.demo.luckDraw;

import java.math.BigDecimal;
import java.util.*;

/**
 * 抽奖测试类
 *
 * @author zhangxy
 */
public class LuckTest {

    public static void main(String[] args) {
        List<AnniversaryPrizePool> anniversaryPrizePoolsList = new ArrayList<>();
        anniversaryPrizePoolsList.add(new AnniversaryPrizePool(1, new BigDecimal(0.4), 1, "再来一次"));
        anniversaryPrizePoolsList.add(new AnniversaryPrizePool(2, new BigDecimal(0.3), 3, "本站VIP一年"));
        anniversaryPrizePoolsList.add(new AnniversaryPrizePool(3, new BigDecimal(0.5), 0, "谢谢参与"));
        anniversaryPrizePoolsList.add(new AnniversaryPrizePool(4, new BigDecimal(0.2), 5, "50金币"));
        anniversaryPrizePoolsList.add(new AnniversaryPrizePool(5, new BigDecimal(0.1), 6, "Iphone 6"));
        anniversaryPrizePoolsList.add(new AnniversaryPrizePool(6, new BigDecimal(0), 7, "Ipad Air2"));
        anniversaryPrizePoolsList.add(new AnniversaryPrizePool(7, new BigDecimal(0), 8, "100元手机话费"));
        double num = 100;
        for (int i = 0; i < num; i++) {
            int lottery = lottery(anniversaryPrizePoolsList);
            System.out.println("第" + i + "抽奖，奖品" + lottery);
        }
    }


    /**
     * 抽奖
     * @param anniversaryPrizePools 原始的概率列表，保证顺序和实际物品对应
     * @return 物品的索引
     */
    public static int lottery(List<AnniversaryPrizePool> anniversaryPrizePools) {
        //扩大倍数
        int mulriple = 100;
        BigDecimal now = BigDecimal.ZERO;
        for (AnniversaryPrizePool anniversaryPrizePool : anniversaryPrizePools) {
            //所有概率加起来
            now = now.add(anniversaryPrizePool.getWinningProbability());
        }
        //概率乘以倍数
        mulriple = now.multiply(new BigDecimal(mulriple)).intValue();
        int lastScope = 0;
        // 洗牌，打乱奖品次序
        Collections.shuffle(anniversaryPrizePools);
        Map<Integer, int[]> prizeScopes = new HashMap<>(anniversaryPrizePools.size());
        for (AnniversaryPrizePool anniversaryPrizePool : anniversaryPrizePools) {
            int prizeId = anniversaryPrizePool.getPrizeId();
            // 划分区间
            int currentScope = lastScope + anniversaryPrizePool.getWinningProbability().multiply((new BigDecimal(mulriple).divide(now,0,BigDecimal.ROUND_HALF_UP))).intValue();
            prizeScopes.put(prizeId, new int[] { lastScope + 1, currentScope });
            lastScope = currentScope;
        }
        //获得随机的概率
        int luckyNumber = new Random().nextInt(mulriple);
        int luckyPrizeId = 0;
        // 查找随机数所在的区间
        if ((null != prizeScopes) && !prizeScopes.isEmpty()) {
            Set<Map.Entry<Integer, int[]>> entrySets = prizeScopes.entrySet();
            for (Map.Entry<Integer, int[]> m : entrySets) {
                int key = m.getKey();
                if (luckyNumber >= m.getValue()[0] && luckyNumber <= m.getValue()[1]) {
                    luckyPrizeId = key;
                    break;
                }
            }
        }
        return luckyPrizeId;
    }






}
